package com.regal.packet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;

//import com.regal.newspush.NewsPushPacket.NewsItem;

/**
 *
 */
public class CompanyList extends IQ {

	private final List<CompanyItem> comItems = new ArrayList<CompanyItem>();


    /**
     * Element name of the packet extension.
     */
    public static final String ELEMENT_NAME = "query";

    /**
     * Namespace of the packet extension.
     */
    public static final String NAMESPACE = "jabber:iq:company_list";
    
    public void addCompanyItem(CompanyItem item) {    	
    	synchronized (comItems) {
    		comItems.add(item);
        }    	
    }
    
    public int getCompanyItemCount() {
        synchronized (comItems) {
            return comItems.size();
        }
    }
    
    public Collection<CompanyItem> getCompanyItems() {
        synchronized (comItems) {
            return Collections.unmodifiableList(new ArrayList<CompanyItem>(comItems));
        }
    }

    public String getChildElementXML() {
        StringBuffer buf = new StringBuffer();
        buf.append("<query xmlns=\"" + NAMESPACE + "\">");
        buf.append("</query>");
        return buf.toString();
        
                
    }

    
    
    /**
     * A Company item, which consists of a companyid...
     */
    public static class CompanyItem {

    	private String tradeid;
    	private String tradename;
    	private String companyid;
    	private String companyname;
    	private String phoneno;
    	private String fax;
    	private String address;

        
    	public String getTradename() {
			return tradename;
		}
		public void setTradename(String tradename) {
			this.tradename = tradename;
		}
		public CompanyItem() {
        
        }
        /**
         * Creates a new company item.
         *
         * @param user the companyid.
         * @param name the companyname.
         */
        public CompanyItem(String companyid, String companyname, String phoneno, String fax, String address) {
            this.companyid = companyid;
            this.companyname = companyname;
            this.phoneno = phoneno;
        	this.fax = fax;
        	this.address = address;
        }


		public String getAddress() {
			return address;
		}


		public void setAddress(String address) {
			this.address = address;
		}


		public String getCompanyid() {
			return companyid;
		}


		public void setCompanyid(String companyid) {
			this.companyid = companyid;
		}


		public String getCompanyname() {
			return companyname;
		}


		public void setCompanyname(String companyname) {
			this.companyname = companyname;
		}


		public String getFax() {
			return fax;
		}


		public void setFax(String fax) {
			this.fax = fax;
		}


		public String getPhoneno() {
			return phoneno;
		}


		public void setPhoneno(String phoneno) {
			this.phoneno = phoneno;
		}
		public String getTradeid() {
			return tradeid;
		}
		public void setTradeid(String tradeid) {
			this.tradeid = tradeid;
		}
    }
    /**
     * An IQProvider for CompanyListIQ packet.
     *
     * @author Derek DeMoro
     */
    public static class Provider implements IQProvider {

        public Provider() {
            super();
        }

        public IQ parseIQ(XmlPullParser parser) throws Exception {
            
        	CompanyList comList = new CompanyList();
        	CompanyItem comItem = null;

            boolean done = false;
            while (!done) {
                int eventType = parser.next();
                if (eventType == XmlPullParser.START_TAG) {
                	if (parser.getName().equals("item")) {
                		comItem = new CompanyItem();
                		String tradeId = parser.getAttributeValue("", "tradeid");
                        comItem.setTradeid(tradeId);
                        String tradeName = parser.getAttributeValue("", "tradename");
                        comItem.setTradename(tradeName);
                        String companyId = parser.getAttributeValue("", "companyid");
                        comItem.setCompanyid(companyId);
                        String companyName = parser.getAttributeValue("", "companyname");
                        comItem.setCompanyname(companyName);
                        String phoneNo = parser.getAttributeValue("", "phoneno");
                        comItem.setPhoneno(phoneNo);
                        String fax = parser.getAttributeValue("", "fax");
                        comItem.setFax(fax);
                        String address = parser.getAttributeValue("", "address");
                        comItem.setAddress(address);
                    }
                }

                else if (eventType == XmlPullParser.END_TAG) {
                	if (parser.getName().equals("item")) {
                		comList.addCompanyItem(comItem);
                		System.out.println("get company item :"+comItem.getCompanyname()+";"+comItem.getCompanyid());
                    }
                    if (parser.getName().equals(ELEMENT_NAME)) {
                        done = true;
                    }
                }
            }
            return comList;
        }
    }

   
}
